package club.itstudio.itouc.controller.admin

import club.itstudio.itouc.config.AdminConfig
import club.itstudio.itouc.core.util.Resource
import club.itstudio.itouc.jw.JwHost
import club.itstudio.itouc.service.JwOps
import org.springframework.web.bind.annotation.CookieValue
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import javax.servlet.http.HttpServletResponse

@RestController
@RequestMapping("/admin")
class AdminIndex(val resource: Resource,
                 val adminConfig: AdminConfig,
                 val jwOps: JwOps) {

    @GetMapping("/index")
    fun adminIndex(@CookieValue token: String,
                   response: HttpServletResponse): String {
        val session = jwOps.getJwSession(token)
        var res = ""
        if (adminConfig.contains(session.userCode))
            res = resource.template("admin/index.html")
        else
            response.sendRedirect(JwHost)
        return res
    }
}